<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2019/5/20
 * Time: 11:14
 */

namespace app\index\controller;


use app\admin\model\AssembleList;
use app\admin\model\AssembleUser;
use app\admin\model\CityRange;
use app\admin\model\GoodsList;
use app\admin\model\OrderList;
use app\admin\model\WechatConfig;
use think\facade\Session;

class Goods extends Base
{

    public function goodsList(){
        return $this->fetch();
    }
    public function goodsListData($is_home = 0){
        $page = input('post.page');
        $limit = input('post.limit');
        $where = $is_home?['is_home'=>1]:"";
        $data = GoodsList::where('status',1)->where($where)->order('sort asc')
            ->field('id,good_name,image,price,old_price,subscription')->page($page,$limit)->select()->each(function ($item){
            $assemble = AssembleList::where('good_id',$item->id)->where('status',1)->find();
            $item->assemble = $assemble?$assemble:false;
        })->toArray();
        $count = GoodsList::where('status',1)->where($where)->count();
        if (!$data){
            return json(['code'=>0,'msg'=>'无数据','data'=>$data,'count'=>$count]);
        }
        return json(['code'=>1,'msg'=>'返回成功','data'=>$data,'count'=>$count]);
    }
    public function goodsInfo($id){
        $data = GoodsList::where('id',$id)->find();
        $data['images'] = explode(',',$data['images']);
        $data['range_id'] = CityRange::where('id','in',$data['range_id'])->column('name');
        $tel = WechatConfig::where('wx_id',1)->value('phone');
        $this->assign('data',$data);
        $this->assign('tel',$tel);
        return $this->fetch();
    }

    public function userInfo($id){
        $this->assign('ass_id',$id);
        return $this->fetch();
    }


    public function orderDown(){
        $post = input('post.');
        $validate = $this->validate($post,'app\index\validate\User');
        if (true !== $validate){
            if($validate=='表单令牌过期'){
                $token = $this->request->token('__token__', 'sha1');
                return json(['code'=>1000,'msg'=>'你已提交过了!','__token__'=>$token]);
            }
            return json(['code'=>0,'msg'=>$validate]);
        }
        $find = GoodsList::where('id',$post['ass_id'])->find();
        if(!$find){
            return json(['code'=>0,'msg'=>'商品不存在']);
        }
        if($post['code']!=cookie('code')){
            return json(['code'=>0,'msg'=>'验证码错误！']);
        }
        $data=[
            'order_no'=>$this->randomStr().time(),
            'user_id'=>Session::get('user_id'),
            'phone'=>$post['phone'],
            'true_name'=>$post['true_name'],
            'good_id'=>$find['id'],
            'good_name'=>$find['good_name'],
            'image'=>$find['image'],
            'sign_type'=>$find['driver_id'],
            'pay_money'=>$find['price'],
            'pay_type'=>0,
            'test_type'=>1,
            'create_time'=>time(),
            'order_type'=>1,
            'id_card'=>$post['id_card']
        ];
        $save=OrderList::insertGetId($data);
        if($save){
            cookie('code',null);
            \app\admin\model\User::update(['phone'=>$post['phone'],'id_card'=>$post['id_card'],'true_name'=>$post['true_name']],['id'=>$find['id']]);
            $url='/index/wxpay/goodsPay?id='.$save.'&type=0';
            return json(['code'=>1,'msg'=>'提交成功！','jump_url'=>$url]);
        }
        return json(['code'=>0,'msg'=>'提交失败！']);
    }


    public function orderListPay($order_id){
        $ret = OrderList::where('id',$order_id)->find();
        if ($ret['order_type'] == 2){
            $id = $ret['f_id']?$ret['f_id']:$ret['id'];
            $ass = AssembleUser::where('order_id',$id)->find();
            if ($ass && $ass['number'] == 0){
                return json(['code'=>0,'msg'=>'拼团人数已满，请重新下单']);
            }
        }
        $url='/index/wxpay/goodsPay?id='.$order_id.'&type=0';
        return json(['code'=>1,'msg'=>'跳转支付','jump_url'=>$url]);
    }

    public function orderDel($order_id){
        $ret = OrderList::destroy($order_id);
        return json($ret);
    }

}